/*
 * HsqldbServerUtil.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the web-geek, Version 1.0 (the "License");
 *
 */
package org.geek.common.util;

import java.io.File;
import java.util.Set;

import org.hsqldb.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**   
 * <p>启动Hsqldb数据库</p>
 * @author ninglong bhd2973@qq.com   
 * @version V1.0   
 */
public class HsqldbServerUtil {

	 private static final Logger logger = LoggerFactory.getLogger(HsqldbServerUtil.class);
	 
	 public static final int DEFAULT_PORT = 9001;
	 public static final String DEFAULT_ADDRESS = "127.0.0.1";
	 
	 private Set<String> databaseNames;
	 
	 // database server properties
	 private String address;
	 private int port;
	 private boolean silent;
	 private boolean trace;
	 
	 private Server hsqldbServer;
	 
	 public HsqldbServerUtil(){
		  super();
		  address = DEFAULT_ADDRESS;
		  port = DEFAULT_PORT;
		  silent = true;
		  trace = false;
	 }
	 
	public void initialize() {
		logger.debug("starts to initializing hsqldb server");
		
		final String databaseScriptRootPath = getFileDirectory();
		
		logger.debug("hsqldb script root path " + databaseScriptRootPath);
		hsqldbServer = new Server();
		hsqldbServer.setPort(port);
		hsqldbServer.setAddress(address);
		hsqldbServer.setSilent(silent);
		hsqldbServer.setTrace(trace);
		
		setsetDatabaseNames(databaseNames, hsqldbServer, databaseScriptRootPath);
		try {
			hsqldbServer.start();
			logger.debug("hsqldb server started, hsqldb server state is " + hsqldbServer.getStateDescriptor());
		} catch (Exception e) {
			logger.error("can not start hsqldb server", e);
			throw new RuntimeException("can not start hsqldb server", e);
		}
	}
	 
	private void setsetDatabaseNames(Set<String> names, Server server,
			String scriptPath) {
		if (names == null || names.isEmpty()) {
			throw new IllegalArgumentException("can not get database name");
		}
		int count = 0;
		for (String name : names) {
			logger.debug("setting [" + count++ + "] which database name is '" + name + "'");
			hsqldbServer.setDatabaseName(count, name);
			hsqldbServer.setDatabasePath(count, scriptPath + name);
		}
	}
	 
	 private String getFileDirectory() {
		  File classDirectory = new File(this.getClass().getResource("/").getPath());
		  return classDirectory.getParent() + "/hsqldb/";
	 }
	 
	public void destory() {
		try {
			hsqldbServer.stop();
			logger.debug("hsqldb server stoped");
		} catch (Exception e) {
			logger.error("can not stop hsqldb server", e);
		}
	}

	 public Set<String> getDatabaseNames() {
		 return databaseNames;
	 }

	 public void setDatabaseNames(Set<String> databaseNames) {
		 this.databaseNames = databaseNames;
	 }

	 public String getAddress() {
		 return address;
	 }

	 public void setAddress(String address) {
		 this.address = address;
	 }

	 public int getPort() {
		 return port;
	 }

	 public void setPort(int port) {
		 this.port = port;
	 }

	 public boolean isSilent() {
		 return silent;
	 }

	 public void setSilent(boolean silent) {
		 this.silent = silent;
	 }

	 public boolean isTrace() {
		 return trace;
	 }

	 public void setTrace(boolean trace) {
		 this.trace = trace;
	 }
}